<?php
/**
 * Created by IntelliJ IDEA.
 * User: 张伯发
 * Date: 2019/8/2
 * Time: 10:58
 */

namespace App\Common\Http;

class Response
{
    const CODE_DEFAULT_SUCCESS  = 0;
    const CODE_DEFAULT_ERROR    = -1;



    const KEY_TEXT_CODE         = 'code';
    const KEY_TEXT_DATA         = 'data';
    const KEY_TEXT_MSG          = 'msg';

    const TEXT_DEFAULT_SUCCESS  = 'success';
    const TEXT_DEFAULT_ERROR    = 'error';


    const TEXT_SYSTEM_ERROR     = 'system error';











    public static function fail(string $msg = self::TEXT_DEFAULT_ERROR,
                                int $code = self::CODE_DEFAULT_ERROR,
                                $data = null): array
    {
        return self::format($code, $data, $msg);
    }


    public static function success($data = null,
                                   int $code = self::CODE_DEFAULT_SUCCESS,
                                   string $msg = self::TEXT_DEFAULT_SUCCESS): array
    {
        return self::format($code, $data, $msg);
    }

    /**
     * @param int $code
     * @param $data
     * @param string $msg
     * @return array
     * @author 张伯发 2019/8/2 11:05
     */
    private static function format(int $code, $data, string $msg) : array
    {
        $response[self::KEY_TEXT_CODE] = $code;
        $response[self::KEY_TEXT_DATA] = $data;
        $response[self::KEY_TEXT_MSG] = $msg;

        return $response;
    }


}
